package cn.com.gridinfo.sooc.core.expand.feedback.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.feedback.CodeConst;
import cn.com.gridinfo.sooc.core.expand.feedback.activity.FeedbackInfoActivity;
import cn.com.gridinfo.sooc.core.expand.feedback.adapter.MySuggestionAdapter;
import cn.com.gridinfo.sooc.core.expand.feedback.bean.Suggestion;
import cn.com.gridinfo.sooc.core.expand.feedback.dao.SuggestionDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 我的反馈
 * Created by yingchunli on 2015/12/22.
 */
public class MySuggestionFragment extends BaseSoocFragment {

    View view;//当前界面
    @InjectView(R.id.mysuggestion_list)
    UltimateRecyclerView suggestion_list;//意见列表
    LinearLayoutManager linearLayoutManager;//布局管理器
    MySuggestionAdapter adapter;//适配器
    SuggestionDao dao;
    int maxLastPositon;//数据最后位置
    private static final int INTENT_FEEDBACKINFO = 2;//跳转到反馈详情的常量
    int intentPosition;//跳转的item的位置
    @InjectView(R.id.blank)
    RelativeLayout blank;//空白页

    public static MySuggestionFragment newInstance(String info) {
        return new MySuggestionFragment();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        //判断Fragment中的ListView时候存在，判断该Fragment时候已经正在前台显示  通过这两个判断，就可以知道什么时候去加载数据了
        if (isVisibleToUser && isVisible()) {
            initData(); //加载数据的方法
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    void initData() {
        PageConst.pageNo = 1;
        PageConst.pageSize = 10;
        //发送请求  获取列表
        dao.getSuggestionList("1", null, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_FEEDBACK_MYSUGGESTION_FIRST);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.expand_feedback_mysuggestion, null);
        ButterKnife.inject(this, view);
        dao = new SuggestionDao(this);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        suggestion_list.setLayoutManager(linearLayoutManager);
        setBlank();
        return view;
    }

    /**
     * 空白页的设置
     */
    void setBlank() {
        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.tip_studentmenu);
        linearLayout.setVisibility(View.GONE);
        TextView textView = (TextView) view.findViewById(R.id.tip_feedback);
        textView.setVisibility(View.VISIBLE);
    }

    /**
     * 初始化数据
     */
    void initView() {
        adapter = new MySuggestionAdapter(dao.getSuggestionList());
        suggestion_list.setAdapter(adapter);
        suggestion_list.enableLoadmore();
        suggestion_list.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                PageConst.pageNo = 1;
                dao.getSuggestionList("1", null, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_FEEDBACK_MYSUGGESTION_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        suggestion_list.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                if (itemsCount > (PageConst.pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    dao.getSuggestionList("1", null, ++PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_FEEDBACK_MYSUGGESTION_PULLUP);
                } else {
                    adapter.loadNoDataView();
                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                }
            }
        });
        adapter.setmOnItemClickLitener(new MySuggestionAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(int position, Suggestion suggestion) {
                //跳转到反馈详情界面
                intentPosition = position;
                Intent intent = new Intent(getActivity(), FeedbackInfoActivity.class);
                intent.putExtra("id", suggestion.getId());
                startActivityForResult(intent, INTENT_FEEDBACKINFO);
                AnimUtil.intentSlidIn(getActivity());

            }
        });
    }

    /**
     * 获取返回界面的数据
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        int hfcount = Integer.parseInt(data.getStringExtra("hfcount"));
        if (Integer.parseInt(adapter.suggestionList.get(intentPosition).getReplycount()) != hfcount) {
            adapter.suggestionList.get(intentPosition).setReplycount(hfcount + "");
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_FEEDBACK_MYSUGGESTION_FIRST:
                        //获取我的意见列表返回成功
                        initView();
                        if (dao.getSuggestionList() != null && !dao.getSuggestionList().isEmpty()) {
                            blank.setVisibility(View.GONE);
                            suggestion_list.hideEmptyView();
                        } else {
                            blank.setVisibility(View.VISIBLE);
                            suggestion_list.showEmptyView();
                        }
                        break;
                    case CodeConst.EXPAND_FEEDBACK_MYSUGGESTION_PUSHDOWN:
                        suggestion_list.setRefreshing(false);
                        if (dao.getSuggestionList() != null && !dao.getSuggestionList().isEmpty()) {

                        } else {
                            //无数据处理
                            suggestion_list.showEmptyView();
                        }
                        //刷新数据
                        adapter.refresh(dao.getSuggestionList());
                        if (dao.getSuggestionList().size() > (PageConst.pageSize - 1))
                            //数据大于每页显示的条数,则重新开启加载更多
                            adapter.loadMoreDataView();
                        else {
                            //数据小于当前页的话则不显示加载更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                    case CodeConst.EXPAND_FEEDBACK_MYSUGGESTION_PULLUP:
                        if (dao.getSuggestionList() != null && !dao.getSuggestionList().isEmpty()) {
                            //加载更多
                            adapter.loadmore(dao.getSuggestionList());
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                }
            } else {
                MessageUtils.showRightImageShortToast(getActivity(), dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(getActivity(), "数据请求异常");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
